package com.lebang.controller.setting;

import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.model.setting.Cash;
import com.lebang.service.setting.CashService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author hwq
 * 提现金额设置控制层
 */
@Controller
@RequestMapping("cash")
public class CashController {
    @Autowired
    private CashService cashService;
    /**
     * 进入后台查看提现金额页面
     */
    @Login
    @GetMapping("/list")
    public String list(){
        return "/cash/list";
    }
    /**
     * 提现金额页面分页接口
     */
    @Login
    @PostMapping("/data")
    @ResponseBody
    public Map<String, Object> data (HttpServletRequest request, Cash vo){
        return cashService.pagging(vo);
    }

    /**
     * @param request
     * @param vo
     * @return
     * 新增修改接口
     */
    @PostMapping("/edit")
    @ResponseBody@Login
    @ConfigRecord
    public JsonData edit( Cash vo,HttpServletRequest request){
        return cashService.edit(vo);
    }

    /**
     * @param request
     * @param id
     * @param model
     * @return
     * 根据id查找单条数据
     */
    @GetMapping("/edit")@Login
    public String editlist(HttpServletRequest request, Integer id, Model model){
        if(null!=id){
            model.addAttribute("item",cashService.selectByPrimaryKey(id));
        }else {
            model.addAttribute("item",new Cash());
        }
        return "/cash/edit";
    }

    /**
     * @param request
     * @param id
     * @return
     * 根据id物理删除
     */
    @PostMapping("/delete")
    @ResponseBody@Login
    public JsonData del(HttpServletRequest request,Integer id){
        return cashService.del(id);
    }

    @PostMapping("/cashdata")
    @ResponseBody
    public JsonData del(HttpServletRequest request){
        List<BigDecimal> cash=null;
        cash=cashService.findCashList();
        if(cash==null){
            return Callback.fail("200","获取失败",null);
        }else {
            return Callback.success("200","获取成功",cash);
        }

    }
    @PostMapping("/cashdata/v1")
    @ResponseBody
    public JsonData findlist(HttpServletRequest request){
        List<Cash> cash=null;
        cash=cashService.findlist();
        if(cash==null){
            return Callback.fail("200","获取失败",null);
        }else {
            return Callback.success("200","获取成功",cash);
        }

    }
}
